package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.oa.feign.IStatisticsClient;
import org.springblade.pm.entity.CostPlan;
import org.springblade.pm.entity.ManpowerPlan;
import org.springblade.pm.service.ICostPlanService;
import org.springblade.pm.service.IManpowerPlanService;
import org.springblade.pm.vo.ManpowerPlanVo;
import org.springblade.pm.wrappper.ManpowerPlanWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 成本管理
 */
@RestController
@RequestMapping("cost")
@AllArgsConstructor
public class CostController {

	private ICostPlanService costPlanService;
	private IManpowerPlanService manpowerPlanService;
	private IStatisticsClient statisticsClient;
	@PostMapping("/submit")
	@ApiOperation(value = "新增计划人力")
	public R submit(ManpowerPlan manpowerPlan, BladeUser bladeUser) {
		if(manpowerPlan.getId()==null||"".equals(manpowerPlan.getId())){
			manpowerPlan.setCreateTime(new Date());
			manpowerPlan.setCreateUser(bladeUser.getUserId());
		}
		return R.status(manpowerPlanService.saveOrUpdate(manpowerPlan));

	}
	@PostMapping("/submitCostPlan")
	@ApiOperation(value = "新增预计费用")
	public R submitCostPlan(CostPlan costPlan, BladeUser bladeUser) {
		if(costPlan.getId()==null||"".equals(costPlan.getId())){
			costPlan.setCreateTime(new Date());
			costPlan.setCreateUser(bladeUser.getUserId());
		}
		return R.status(costPlanService.saveOrUpdate(costPlan));
	}

	@GetMapping("/deleteManpowerPlan")
	@ApiOperation(value = "删除计划人力")
	public R deleteManpowerPlan(Integer id) {
		return R.status(manpowerPlanService.removeById(id));
	}

	@GetMapping("/manpowerPlanList")
	@ApiOperation(value = "人力成本列表")
	public R<List<Map<String,Object>>> manpowerPlanList(Integer projectId,String projectCode) {
		QueryWrapper<ManpowerPlan> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",projectId).select("sum(plan_manpower) planManpower","sum(persons) persons","user_post").groupBy("user_post");
		List<ManpowerPlan> ManpowerPlans = manpowerPlanService.list(wrapper);
		List<ManpowerPlanVo> manpowerPlanVos = ManpowerPlanWrapper.build().entityVoList(ManpowerPlans);
		List<Map<String, Object>> list = manpowerPlanService.getUserHrData(projectCode);

		for(Map<String,Object> map:list){
			for (ManpowerPlanVo manpowerPlanVo:manpowerPlanVos){
				if(manpowerPlanVo.getUserPostName().equals(map.get("roleName").toString())){
						map.put("persons",manpowerPlanVo.getPersons());
						map.put("planManpower",manpowerPlanVo.getPlanManpower());
						manpowerPlanVos.remove(manpowerPlanVo);
						break;
				}
			}
		}
		for (ManpowerPlanVo manpowerPlanVo:manpowerPlanVos){
			HashMap<String, Object> map = new HashMap<>();;
			map.put("roleName",manpowerPlanVo.getUserPostName());
			map.put("persons",manpowerPlanVo.getPersons());
			map.put("planManpower",manpowerPlanVo.getPlanManpower());
			map.put("realUser",null);
			map.put("hrData",0);
			list.add(map);
		}
		List<Map<String, Object>> roleHrData = manpowerPlanService.getRoleHrData(projectCode);
		for(Map<String,Object> map1:list){
			for(Map<String,Object> map2:roleHrData){
				if(map1.get("roleName").toString().equals(map2.get("roleName").toString())&&map1.get("planManpower")!=null){
					map1.put("deviation",Float.parseFloat(map1.get("planManpower").toString())-Float.parseFloat(map2.get("hrData").toString()));
				}
			}
		}
		return R.data(list);
	}


	/**
	 * 获取差旅、团建、其他的预计和消耗费用，柱状图
	 * @return
	 */
	@GetMapping("/getCost")
	@ApiOperation(value = "获取差旅、团建、其他的预计和消耗费用，柱状图")
	public R getCost(String  projectCode,Integer projectId){
		Map<String, String> fyMap = statisticsClient.getCostByProjectId(projectCode).getData();
		QueryWrapper<CostPlan> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",projectId).select("IFNULL(stroke_cost,0) strokeCost","IFNULL(team_cost,0) teamCost","IFNULL(other_cost,0) otherCost");
		CostPlan one = costPlanService.getOne(wrapper);
		if (one==null){
			BigDecimal bigDecimal = new BigDecimal(0);
			one=new CostPlan(bigDecimal,bigDecimal,bigDecimal);
		}
		List<Object> fyList = new ArrayList<>();
		//已消耗差旅（cl），团建(tj),其他费用(other)
		HashMap<String, Object> map = new HashMap<>();
		List<String> list = fyMap.values().stream().collect(Collectors.toList());
		map.put("data",list);
		map.put("name","已消耗费用");
		fyList.add(map);
		//计划差旅（cl），团建(tj),其他费用(other)
		HashMap<String, Object> planMap = new HashMap<>();
		planMap.put("name","预计费用");
		ArrayList<String> planList = new ArrayList<>();
		planList.add(one.getStrokeCost().toString());
		planList.add(one.getTeamCost().toString());
		planList.add(one.getOtherCost().toString());
		planMap.put("data",planList);
		fyList.add(planMap);

		ArrayList<String> nameList = new ArrayList<>();
		nameList.add("差旅");
		nameList.add("团建");
		nameList.add("其他");

		HashMap<String, Object> HistogramMap = new HashMap<>();
		Double fysum = list.stream().collect(Collectors.summingDouble(Double::valueOf));
		Double planSum = planList.stream().collect(Collectors.summingDouble(Double::valueOf));

		if (fysum+planSum>0.00f){
			HistogramMap.put("series",fyList);
		}else{
			HistogramMap.put("series",null);
		}

		HistogramMap.put("x",nameList);
		return R.data(HistogramMap);
	}


	/**
	 * 获取项目各项预计和消耗的成本
	 * @return
	 */
	@GetMapping("/getCostList")
	@ApiOperation(value = "获取各项预计和消耗的成本")
	public R getCostList(String  projectCode,Integer projectId){
		Map<String, String> costMap = new HashMap<>();

		Map<String, String> data = statisticsClient.getCostByProjectId(projectCode).getData();
		double costSum = data.values().stream().collect(Collectors.summarizingDouble(x -> Double.valueOf(x).doubleValue())).getSum();
		costMap.put("costSum",Double.valueOf(costSum).toString());//已消耗费用成本
		String projectHrData = manpowerPlanService.getProjectHrData(projectCode);
		costMap.put("HrDataSum",projectHrData);//已消耗人力成本
		QueryWrapper<ManpowerPlan> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id",projectId);
		List<ManpowerPlan> list = manpowerPlanService.list(wrapper);
		Double collect = list.stream().collect(Collectors.summingDouble(ManpowerPlan::getPlanManpower));
		costMap.put("HrDataPlanSum",collect.toString());//人力预计消耗成本
		QueryWrapper<CostPlan> wrapper1 = new QueryWrapper<>();
		wrapper1.eq("project_id",projectId).select("IFNULL(stroke_cost,0) strokeCost","IFNULL(team_cost,0) teamCost","IFNULL(other_cost,0) otherCost");
		CostPlan one = costPlanService.getOne(wrapper1);
		BigDecimal add =new BigDecimal(0);
		if(one!=null){
			add = one.getStrokeCost().add(one.getTeamCost().add(one.getOtherCost()));
		}
		costMap.put("costPlanSum",add.toString());//计划费用总和
		return R.data(costMap);
	}

}
